#include "mycoin.h"
#include <qdebug.h>

MyCoin::MyCoin(QWidget *parent) : QPushButton(parent)
{

}

MyCoin::MyCoin(QString btnImg)
{
    QPixmap pix;
    bool ret = pix.load(btnImg);
    if(!ret) {
        qDebug()<< "图片加载失败："<< btnImg;
        return;
    }

    setFixedSize(pix.width(), pix.height());

    // 设置不规划图片样式
    this->setStyleSheet("QPushButton{border:0px;}");

    // 设置图标
    this->setIcon(pix);

    // 设置图标大小
    this->setIconSize({pix.width(), pix.height()});

    // 初始化定时器
    timer1 = new QTimer(this);
    timer2 = new QTimer(this);

    connect(timer1, &QTimer::timeout, [=](){
        QPixmap pix;
        pix.load(QString( ":/res/Coin000%1.png").arg(min++));

        setFixedSize(pix.width(), pix.height());

        // 设置不规划图片样式
        this->setStyleSheet("QPushButton{border:0px;}");

        // 设置图标
        this->setIcon(pix);

        // 设置图标大小
        this->setIconSize({pix.width(), pix.height()});
        if(this->min>this->max) {
            timer1->stop();
            isAnimation=false;
        }
    });


    connect(timer2, &QTimer::timeout, [=](){
        QPixmap pix;
        pix.load(QString( ":/res/Coin000%1.png").arg(max--));

        setFixedSize(pix.width(), pix.height());

        // 设置不规划图片样式
        this->setStyleSheet("QPushButton{border:0px;}");

        // 设置图标
        this->setIcon(pix);

        // 设置图标大小
        this->setIconSize({pix.width(), pix.height()});
        if(this->min>this->max) {
            timer2->stop();
            isAnimation=false;
        }
    });
}



void MyCoin::changeFlag()
{
    isAnimation=true;
    min =1, max=8;
    if(this->flag) {
        timer1->start(30);
    }else {
        timer2->start(30);
    }

    this->flag ^=1;
}

void MyCoin::mousePressEvent(QMouseEvent *e)
{
    qDebug()<<"金币点击";
    if(isAnimation || isWin)return;
    QPushButton::mousePressEvent(e);
}
